//
//  SYSetViewController.m
//  BSHOUYIFANG
//
//  Created by Zhoudi on 15/8/14.
//  Copyright (c) 2015年 CYB. All rights reserved.
//

#import "SYSetViewController.h"
#import "Header.h"
#import "SYFileHandle.h"
#import "SYProtocolViewController.h"
#import "SYSuggestionViewController.h"
#import "SYSetargeement.h"
@interface SYSetViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,strong)UITableView *tableView;
@property(nonatomic,strong)NSArray *imageArray;
@property(nonatomic,strong)NSArray *titleLabelArray;

@end

@implementation SYSetViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self navigationItemTitleColor];
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT) style:UITableViewStyleGrouped];
    [self.view addSubview:_tableView];
    _tableView.delegate =self;
    _tableView.dataSource =self;
    [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    _imageArray =@[@"setting_feedback@2x.png",@"setting_check_update@2x.png",@"setting_vote@2x.png",@"setting_eula@2x.png",@"clear_cache@2x.png"];
    _titleLabelArray =@[@"意见反馈",@"给我评分",@"用户协议",@"清理缓存"];
    //_tableView.scrollEnabled = NO;
    UIButton*leftButton = [[UIButton alloc]initWithFrame:CGRectMake(0,0,30*0.6,30*0.6)];
    [leftButton setImage:[UIImage imageNamed:@"iconfont-fanhui"]forState:UIControlStateNormal];
    [leftButton addTarget:self action:@selector(searchprogram) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem*leftItem = [[UIBarButtonItem alloc]initWithCustomView:leftButton];
    self.navigationItem.leftBarButtonItem= leftItem;
    // Do any additional setup after loading the view.
}
- (void)searchprogram{
    [self.navigationController popToRootViewControllerAnimated:YES];
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.01;
}


-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 4;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    switch (indexPath.row) {
        case 0:
        {   //意见反馈
            [self p_showSuggestion];
        }
            break;
            //        case 1:
            //        {
            //
            //        }
            //            break;
        case 1:
        {
            UIAlertView *alterView = [[UIAlertView alloc]initWithTitle:@"提示" message:@"请您在Appstore评分，谢谢！" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:@"取消", nil];
            [alterView show];
            
        }
            break;
        case 2:
        {   //用户协议
            [self p_showUserAgreement];
        }
            break;
        case 3:
        {
            //清理缓存
            [self clean];
        }
            break;
            
        default:
            break;
    }
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.textLabel.text =_titleLabelArray[indexPath.row];
    cell.imageView.image = [UIImage imageNamed:_imageArray[indexPath.row]];
    if (indexPath.row ==1) {
       // UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(SCREEN_WIDTH -50, 0, 40, cell.frame.size.height)];
       // label.text =@"1.1.3";
        //cell.accessoryView =label;
         cell.accessoryType = 1;
    }else
    {
        cell.accessoryType = 1;
    }
    return cell;
    
}
/**
 *  清理缓存
 */
- (void)clean
{
    BOOL islongin = [[SYFileHandle shareInstance] loginState];
    if (islongin) {
        
        NSString *sandBOX = NSHomeDirectory();
        NSString *temp = [sandBOX stringByAppendingPathComponent:@"temp"];
        NSFileManager *fileManager = [NSFileManager defaultManager];
        [fileManager removeItemAtPath:temp error:nil];
        UIAlertController *alertcontroller = [UIAlertController alertControllerWithTitle:@"提示" message:@"清空完成" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {
            
        }];
        
        [alertcontroller addAction:defaultAction];
        [self presentViewController:alertcontroller animated:YES completion:nil];
    }
}
/**
 *  意见反馈
 */
- (void)p_showSuggestion
{
    SYSuggestionViewController *suggestionVC = [[SYSuggestionViewController alloc] init];
    
    suggestionVC.showBlock =^{
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(SCREEN_WIDTH/3, SCREEN_HEIGHT/2, SCREEN_WIDTH/3, SCREEN_WIDTH/10)];
        label.text =@"感谢您的宝贵\n意见!!";
        label.backgroundColor = [UIColor blackColor];
        label.textColor = [UIColor whiteColor];
        label.textAlignment =NSTextAlignmentCenter;
        label.numberOfLines =2;
        label.font = [UIFont systemFontOfSize:13];
        [self.view addSubview:label];
        [label performSelector:@selector(removeFromSuperview) withObject:nil afterDelay:2];
    };
    [self.navigationController pushViewController:suggestionVC animated:YES];
}

- (void)navigationItemTitleColor
{
    UILabel *customLab = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 30)];
    [customLab setTextColor:[UIColor whiteColor]];
    [customLab setText:@"设 置"];
    customLab.textAlignment = NSTextAlignmentCenter;
    customLab.font = [UIFont boldSystemFontOfSize:20];
    self.navigationItem.titleView = customLab;
}

/**
 *  用户协议
 */
- (void)p_showUserAgreement
{
    SYProtocolViewController *protocolVC = [[SYProtocolViewController alloc] init];
    [self.navigationController pushViewController:protocolVC animated:YES];
}


- (void)viewWillAppear:(BOOL)animated{
    self.tabBarController.tabBar.translucent = NO;
    self.tabBarController.tabBar.hidden = YES;
}

- (void)viewWillDisappear:(BOOL)animated{
    self.tabBarController.tabBar.translucent = YES;
    self.tabBarController.tabBar.hidden = NO;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
